<?php
class WS{
    public $ws = null;
    private $ip = "0.0.0.0";
    private $port = 9502;

    public function __construct($ip = null,$port = null){
        if($ip){
            $this->ip = $ip;
        }
        if($port){
            $this->port = $port;
        }
        $this->ws = new swoole_websocket_server($this->ip,$this->port);
        $this->bind('open',[$this,'onOpen']);
        $this->bind('message',[$this,'onMessage']);
        $this->bind('close',[$this,'onClose']);
    }

    public function onOpen($ws,$request){
        $this->ws->push($request->fd,"Client: ".$request->fd.'connected \n');
    }
    public function onMessage($ws,$frame){
        echo "Receive Message from ".$frame->fd.';data:'.$frame->data."opcode:{$frame->opcode};fin:{$frame->finish} \n";
        $this->ws->push($frame->fd,"server: had got message from client:".$frame->fd."\n");
    }

    public function bind($event,callable $func){
        $this->ws->on($event,$func);
    }

    public function onClose($ws,$fd){
        echo "Client {$fd} had closed \n";
    }

}
$ws = new WS();